#!/usr/local/bin/python2.5

import sys, pottononline.feedparser, urllib
from pottononline.map.models import *
from pottononline.BeautifulSoup import BeautifulSoup
import re


verbose = True

user = User.objects.get(pk=1)


URL='http://www.globrix.com/property/buy/potton||sandy?ns=true&rd=1&br=buy&qt=Potton%2C+Sandy&hits=100&format=rss'
rssfeed = pottononline.feedparser.parse(URL)

def strip_html(htmlstring):
  return re.sub(r'<[^>]*?>', '', htmlstring) 
 

try:
  hfsType = MarkerType.objects.get(name="House for sale")
except:
  print "Error:  You probably have a new database, and haven't created the Marker Type of 'House for sale'."
  sys.exit(-1)

# delete all existing geoPhotos ?
clean_up = raw_input("Delete *ALL* old House?  (y/n) [No]: ")
if clean_up == "y":
  for h in Marker.objects.filter(itemtype=hfsType):
    h.delete()

for item in rssfeed['entries']:
  soup = BeautifulSoup(''.join(item.summary))
  name = soup.find("span", {"class":"address"}).contents[0].strip()
  price = soup.find("span", {"class":"price"}).contents[0].split(';')[1].split('.')[0].replace(',','')
  lon = float(item.geo_long)
  lat = float(item.geo_lat)
  descr = u''
  descr = descr.join(strip_html(item.summary).strip())
  descr = descr.replace('&pound;',u'\xa3')
  descr = descr.replace('\n','')
  link = item.links[0].href
  thumb = soup.find("img", {"class":"hit"}).attrs[0][1]
  
  if verbose:
    print name
    print price
    print lon
    print lat
    print descr
    print thumb
    print "---------------------------"

  house = Marker()
  house.name = name
  house.lat = lat
  house.lon = lon
  house.itemtype = hfsType
  house.price = price
  house.link = link
  house.thumbnail = thumb
  house.description = descr 
  house.owner_user = user
  house.commentable = True
  house.rateable = True
  house.save() 
  
